package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.conn.GuiGuCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory2;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    //查询所有分类数据
    @Override
    public List<AlbumCategoryVo> findAllCategory() {
        return baseCategoryViewMapper.findAllCategory();
    }

    @Override
    @GuiGuCache(
            cacheKey = "album:category:view:" + "#{#params[0]}" ,
            enableLock = true ,
            lockName = "album:category:view:lock:" + "#{#params[0]}"
    )
    public BaseCategoryView findCategoryByCategory3Id(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    //根据一级分类id获取全部分类信息
    @Override
    public AlbumCategoryVo getCategoryList(Long category1Id) {
        return baseCategoryViewMapper.getCategoryList(category1Id);

    }

    //根据一级分类id查询置顶三级分类
    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
// 根据一级分类的id查询所有的二级分类数据
        LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id, category1Id);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);
        List<Long> bastCate2Ids = baseCategory2List.stream().map(baseCategory2 ->
                baseCategory2.getId()
        ).collect(Collectors.toList());

// 根据二级分类的id查询置顶的三级分类数据
        LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id, bastCate2Ids);
        baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop,1) ;
        baseCategory3LambdaQueryWrapper.orderByAsc(BaseCategory3::getId) ;
        baseCategory3LambdaQueryWrapper.select(BaseCategory3::getId , BaseCategory3::getCategory2Id , BaseCategory3::getName, BaseCategory3::getOrderNum , BaseCategory3::getIsTop) ;
        baseCategory3LambdaQueryWrapper.last("limit 7");
        List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);
        return baseCategory3List;

    }


}
